﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Utils;
using System.Threading;
using HostAssistantCore;
using HostAssistantBLL;

namespace HostAssistant
{
    public partial class ftp : U1UserControl
    {
        public ftp()
        {
            InitializeComponent();
        
        }

        #region sitelist_Load

        private void sitelist_Load(object sender, EventArgs e)
        {

            TextBoxToolV2.SetWatermark(txtdbname, "FTP名称");
            var dblist= ftpbll.GetsiteList(null);
            gridControl1.DataSource = dblist;
             
            lblmsg.Text = "当前FTP数量:"+ dblist.Count();


        }

        #endregion

        #region 查询

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            select();
            splashScreenManager1.CloseWaitForm();
            
        }


        void select()
        {
           
          
            var dblist  = ftpbll.GetsiteList(txtdbname.Text);
            gridControl1.DataSource = dblist;

            lblmsg.Text = "当前FTP数量:" + dblist.Count();

        }

        #endregion

        #region 创建mysql


        private void btnadd_Click(object sender, EventArgs e)
        {
          
            //var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            //var mysql = set.FirstOrDefault(o => o.type == "mysql");
            //if (mysql == null)
            //{
              
                
            //    DevExpress.XtraEditors.XtraMessageBox.Show("首次使用请先配置数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //    return;
            //}




            addftp f2 = new addftp();
            f2.ShowDialog(this);
        } 
        #endregion

        #region 列表双击
        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            //if (gridView1.FocusedRowHandle < 0) return;
            //int selectedHandle;
            //selectedHandle = this.gridView1.GetSelectedRows()[0];
            // string  id=  this.gridView1.GetRowCellValue(selectedHandle, "ID").ToString();
            //editdb f2 = new editdb(id);
            //f2.ShowDialog(this);
        }
        #endregion

        #region 创建sql

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mssql");
            if (mysql == null)
            {


                DevExpress.XtraEditors.XtraMessageBox.Show("首次使用请先配置数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            addsqldb f2 = new addsqldb();
            f2.ShowDialog(this);
        }
        #endregion

        #region 删除
        /// <summary>
        /// /*删除*/
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

            if (gridView1.FocusedRowHandle < 0) return;
            int selectedHandle;
            selectedHandle = this.gridView1.GetSelectedRows()[0];
            string id = this.gridView1.GetRowCellValue(selectedHandle, "Name").ToString();

            string Path = this.gridView1.GetRowCellValue(selectedHandle, "Path").ToString();



            if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }
           else
            {
                try
                {

             
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("请等待");
                splashScreenManager1.SetWaitFormDescription("加载中...");


                    ftpbll.ftpdelete(id, Path);
           
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);



                    select();
                  




                }
                catch(Exception ex )
                {
                    NLogger.Error("删除ftp："+ ex.ToString());
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
              
            }


        }
        #endregion

        #region 刷新列表
        /// <summary>
        /// 刷新列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            
            try
            {

                OpHelper.CallWithTimeout(Addmssql, 10000);

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                select();
            }
          


        }
        void Addmssql()
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            ftpbll.Reshftplist();
          
            splashScreenManager1.CloseWaitForm();
        }



        #endregion

        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 查看详情ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (gridView1.FocusedRowHandle < 0) return;
            int selectedHandle;
            selectedHandle = this.gridView1.GetSelectedRows()[0];
            string id = this.gridView1.GetRowCellValue(selectedHandle, "ID").ToString();


            editftp f2 = new editftp(id);
            f2.ShowDialog(this);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 重置密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {


            if (gridView1.FocusedRowHandle < 0) return;
            int selectedHandle;
            selectedHandle = this.gridView1.GetSelectedRows()[0];
            string id = this.gridView1.GetRowCellValue(selectedHandle, "Name").ToString();


            updateftppwd f2 = new updateftppwd(id);
            f2.ShowDialog(this);

        }
    }
}
